package com.ljxt.dt.web.servlet.purchasingmanagementservlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljxt.dt.dao.purchasingmanagementdao.PurchaseInfoDao;
import com.ljxt.dt.dao.purchasingmanagementdao.impl.PurchaseInfoDaoImpl;
import com.ljxt.dt.domain.HandlerInfo;
import com.ljxt.dt.service.purchasingmanagementservice.PurchaseDetailService;
import com.ljxt.dt.service.purchasingmanagementservice.PurchaseInfoService;
import com.ljxt.dt.service.purchasingmanagementservice.impl.PurchaseDetailServiceImpl;
import com.ljxt.dt.service.purchasingmanagementservice.impl.PurchaseInfoServiceImpl;
import com.ljxt.dt.util.xhq.ClassUtil;
import com.ljxt.dt.util.xhq.JdbcUtils;
import com.ljxt.dt.util.xhq.RequestUtil;
import com.ljxt.dt.util.xhq.StringUtil;
import com.ljxt.dt.util.xhq.test.ContinuousTableMapping;
import com.ljxt.dt.vo.JsonResult;
import com.ljxt.dt.vo.PageVO;
import com.ljxt.dt.vo.purchasingmanagement.PurchaseInfoSelect;
import com.ljxt.dt.vo.purchasingmanagement.PurchaseMainVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author 徐浩钦
 * @version 1.0.0
 * @date 2020/5/8 8:27
 * @copyright 老九学堂T11
 */
@WebServlet("/cgdd.html")
public class CgddServlet extends HttpServlet {
    private static PurchaseInfoService purchaseInfoService = new PurchaseInfoServiceImpl();
    private static PurchaseDetailService purchaseDetailService = new PurchaseDetailServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 根据查询的信息查询数据
        PurchaseInfoSelect purchaseInfoSelect = RequestUtil.reqToObj(req,PurchaseInfoSelect.class);
        // 查询所有的订单状态信息
        req.setAttribute("billState",purchaseInfoService.selectBillstateAll());
        req.setAttribute("SupplierInfo",purchaseInfoService.selectSupplierInfoAll());

        // 查询所有的供应商名称信息
        // 获取到分页数据
        String pageIndex = req.getParameter("pageIndex");
        String pageSize = req.getParameter("pageSize");

        // 初始化默认的分页信息设定
        pageIndex = pageIndex == null || pageIndex.isEmpty() ? "1" : pageIndex;
        pageSize = StringUtil.isNullAndEmpty(pageSize) ? "5" : pageSize;

        // 根据分页信息获取数据库数据
        List<PurchaseMainVo> list = purchaseInfoService.selectAll(
        Integer.parseInt(pageIndex),Integer.parseInt(pageSize),purchaseInfoSelect);

        System.out.println(purchaseInfoSelect);
        System.out.println(list);

        // 获取数量
        Integer number = purchaseInfoService.selectCountByAll(purchaseInfoSelect);

        // 创建vo用于相应给客户端
        PageVO pageVO = new PageVO(list,Integer.parseInt(pageIndex),Integer.parseInt(pageSize),number);
        pageVO.setPageURL("cgdd.html");

        System.out.println(pageVO);

        // 用来转换对象的第三方映射对象
        ObjectMapper objectMapper = new ObjectMapper();

        // 如果存在 index 的值，则以页面的方式转发
        if(req.getParameter("init") == null){

            pageVO.setSelectObj(purchaseInfoSelect);
            // 如果不存在则以 json 方式返回
            JsonResult jsonResult = new JsonResult();
            jsonResult.setData(pageVO);
            resp.getWriter().write(objectMapper.writeValueAsString(jsonResult));

        }else{
            req.setAttribute("pageVO",objectMapper.writeValueAsString(pageVO));
            req.getRequestDispatcher("WEB-INF/views/purchasingManagement/cgdd.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("调用了 post");
        System.out.println(req.getParameter("init") == null);
        this.doGet(req, resp);
    }
}
